package ysq.xyg.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.github.pagehelper.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;
import ysq.xyg.dao.ArticleDao;
import ysq.xyg.model.Article;
import ysq.xyg.service.ArticleService;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author yu sheng qiang
 * @create 2017-03-22 15:37
 **/
@Service("articleService")
public class ArticleServiceImpl extends BaseServiceImpl<Article> implements ArticleService {

    @Resource
    private ArticleDao articleDao;

    @Autowired
    public void setBaseDao(ArticleDao articleDao) {
        this.baseDao = articleDao;
    }

    @Override
    public PageInfo<Article> pageInfo(Article article, int pageSize, int pageNum) {
        PageHelper.startPage(pageNum,pageSize);
        //通用Example查询
        PageHelper.orderBy(" order_num desc ");
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();
        if(StringUtil.isNotEmpty(article.getStatus())){
            criteria.andEqualTo("status",article.getStatus());
        }
        if(StringUtil.isNotEmpty(article.getName())){
            criteria.andLike("name","%"+article.getName()+"%");
        }
        if(StringUtil.isNotEmpty(article.getType())){
            criteria.andEqualTo("type",article.getType());
        }
        List<Article> list=articleDao.selectByExample(example);
        return new PageInfo<Article>(list);
    }
    @Override
    public List<Article> list(Article article) {
        //通用Example查询
        Example example = new Example(Article.class);
        Example.Criteria criteria = example.createCriteria();

        if(StringUtil.isNotEmpty(article.getType())){
            criteria.andEqualTo("type",article.getType());
        }
        if(StringUtil.isNotEmpty(article.getStatus())){
            criteria.andEqualTo("status",article.getStatus());
        }
        if(StringUtil.isNotEmpty(article.getName())){
            criteria.andLike("name","%"+article.getName()+"%");
        }


        example.setOrderByClause(" order_num desc ");
        List<Article> list=articleDao.selectByExample(example);
        return list;
    }
}
